#This file will notify order delivery message to new order

load 'get_orders.rb'
load 'user_api.rb'
load 'wechat_api.rb'
load 'holiday.rb'

@rds = Mysql2::Client.new(:host =>ENV['RDS_AGENT'], :username =>"psi_root", :port =>'1403', :password =>ENV['PSI_PASSWORD'], :encoding =>'utf8mb4') if @rds.nil?

#get estimated ship time and estimated arrive time
def get_esat order

    est = '下单后的下一工作日' #'今天'
    order_time = order[:order_time]
    zone_code = order[:zone_code]
    the_day = Date.parse(order_time.strftime('%Y-%m-%d'))
    time_1 = Time.parse(order_time.strftime('%Y-%m-%d 18:00:00'))

    ship =  {'ZB'=>'周边小区','ZT'=>'补拍或自提','ZPP'=>'番禺城区','ZPG'=>'广州城区','KD1'=>'珠三角周边','KD2'=>'广东省内','SW'=>'省外到付'}[zone_code] #如果用 {'a':'b'}, 则要用[:a]

    if is_holiday the_day
        est = '下单后的下一工作日'
    else
        est = '下单当天'
        if zone_code == 'KD1' || zone_code = 'KD2' || zone_code = 'SW'
            est = '下单后的下一工作日' if order_time > time_1
        end
    end
    eat = '发货次日'

    return {:est => est, :eat => eat, :ship => ship}
end

def get_delivery_info order

    info = "#{order[:name]}：您的订单"

    line = order[:line]
    esat = get_esat order


    case line
    when '[X]'
        info += "已整单取消，感谢信任！任何问题请联系丰巢小蜜18998382701，微信同号"
        ship = '已取消订单'
    when '[T]'
        info += "已收到，我们将按该团的【交付时间】，为您安排配送或产地直发。"
        info += "感谢信任！任何问题请联系丰巢小蜜18998382701，微信同号"
        ship = '团购订单'
    when '[Z]'
        info += "已收到，补拍订单将与主订单一起合并配送，自提单请于【#{esat[:est]}】至丰巢仓库自取。"
        info += "感谢信任！任何问题请联系丰巢小蜜18998382701，微信同号"
        ship = '补拍或自提订单'
    else
        info += "已收到，我们将于【#{esat[:est]}】为您进行发货，预计【#{esat[:eat]}】送达，"
        info += "感谢信任！任何问题请联系丰巢小蜜18998382701，微信同号"
        ship = esat[:ship]
    end

    if order[:order_times] == 1
        remark = "【首单提醒】保质期内品质问题无障碍退换，点击详情查看！！\nFOODTRUST®\n让健康和友善触手可及"
    else
        #remark = "常见订单问答请点查看详情\nFOODTRUST® 让健康和友善触手可及"
        remark = "【重要提示】点击详情查看疫情期间服务调整公告！以及新客服通道!\nFOODTRUST®\n让健康和友善触手可及"
    end

    return {:info => info, :remark => remark, :ship=> ship }
    p info
    p remark
    p ship
end

def confirm_orders orders
    text = ''
    printf "confirm orders[ "
    orders.each do |order|

        #send wechat template message
        order_id = order[:order_id]
        uid = order[:uid]
        openid = order[:openid]
        cid = order[:customer_id]
        now = Time.now.strftime('%Y-%m-%d %H:%M:%S')
        info = get_delivery_info order
        info_body = info[:info]
        info_body = "【更新】" + info[:info] if order[:notify_history] == 'resend'
        printf "."
        notification = "#{now} oid##{order_id} 订单配送提示"
        text += "O##{order_id} #{order[:line]} #{order[:zone_code]} #{order[:order_time]} #{order[:addr]}\n"
        text += " #{info[:info]}\n"
        #send_confirm_notice openid, info_body, "#{order[:order_id]} #{sprintf('%.2f',order[:amount])}\n#{order[:addr]}\n#{order[:tel]}", info[:ship], info[:remark], "https://foodtrust.cn/sla/", order[:order_times]

        #send work wechat bot message
        content = "老板们，来单咯~\n"
        list = []
        if order[:order_times]==1
            content = "小蜜，快来接新客哦\n" 
            list = ["foodcherry"] 
        end
        content += ">#{order[:order_id]} RMB#{sprintf('%.2f',order[:amount])}\n
        >#{order[:addr]}\n
        >#{order[:tel]} #{order[:name]}\n"
        send_bot_message content,list

        #update notification history
        comment = order[:notify_history] + " | #{notification}"
        sqlu = "update ogoods.pospal_orders set notify_history='#{comment}' where order_id = '#{order_id}'"
        resu = @rds.query(sqlu)
    end
    printf " ]done\n"
    return text
end

#待将*改为具体columne以提高效率
sql = "select * from ogoods.pospal_orders where line != '[X]' and ( notify_history='' or notify_history like 'test%' or notify_history = 'resend' )"
orders = get_orders_data_by_sql sql
confirm_text = confirm_orders orders
puts confirm_text
